package com.ssyc.student.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.utils.DateUtils;
import com.ssyc.student.R;
import com.ssyc.student.bean.NotifyInfo;

import java.util.List;

/**
 * 学生端-通知消息列表适配器
 * Created by Kevin on 2018/4/20.
 */

public class StNotifyAdapter extends BaseQuickAdapter<NotifyInfo.DataBean, BaseViewHolder> {
    private Context context;
    //需要显示的文字内容
    private String content;

    public StNotifyAdapter(Context context, int layoutResId, @Nullable List<NotifyInfo.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, NotifyInfo.DataBean item) {

        //文字展示信息
        TextView tvContent = helper.getView(R.id.tv_content);

        //图片信息
        ImageView lv = helper.getView(R.id.lv);

        //时间
        TextView tvTime = helper.getView(R.id.tv_time);

        //外层布局
        LinearLayout llOut = helper.getView(R.id.ll_outside);

        //处理需要显示的内容
        if ("1".equals(item.getSub_type())) {
            //对方的名称
            String otherName = item.getName().trim().toString();
            //对方宠物的名称
            String otherePetName = item.getPet_name().trim().toString();
            //自己的宠物名称
            String ownPetName = item.getPet_name2().trim().toString();

            //PK
            if ("1".equals(item.getField1())) {
                //输
                content = otherName + "的宠物" + otherePetName + "与你的宠物" + ownPetName + "进行PK,你输了。";

                //改变图标
                lv.setImageResource(R.drawable.st_notification_fail);

                //改变整体背景
                llOut.setBackgroundResource(R.drawable.st_notification_lose);

            } else if ("0".equals(item.getField1())) {
                //赢
                content = otherName + "的宠物" + otherePetName + "与你的宠物" + ownPetName + "进行PK,你赢了。";
                //改变图标
                //改变图标
                lv.setImageResource(R.drawable.st_notification_win);
                //改变整体背景
                llOut.setBackgroundResource(R.drawable.st_notification_winner);

            } else if ("2".equals(item.getField1())) {
                //平
                content = otherName + "的宠物" + otherePetName + "与你的宠物" + ownPetName + "进行PK,平局。";
                //改变图标
                lv.setImageResource(R.drawable.st_notification_draw_iv);
                //改变整体背景
                llOut.setBackgroundResource(R.drawable.st_notification_draw);
            }

            //计算需要变色的文字位置
            int otherStart = 0;
            int otherEnd = otherName.length();
            int ownStart = otherEnd + 3;
            int ownEnd = ownStart + otherePetName.length();
            int threeStart = ownEnd + 5;
            int threeEnd = threeStart + ownPetName.length();
            //对文字进行变色处理
            setPkTextState(tvContent, content, otherStart, otherEnd, ownStart, ownEnd, threeStart, threeEnd);

        } else if ("2".equals(item.getSub_type())) {
            //拜访
            content = "好友" + item.getName() + "拜访了你。";
            //计算需要变色的文字位置
            int start = 2;
            int end = start + item.getName().length();
            setRedColorForTv(tvContent, start, end);
            //改变图标
            lv.setImageResource(R.drawable.st_notification_pay);
            //改变整体背景
            llOut.setBackgroundResource(R.drawable.st_notification_pay_visit);

        } else if ("3".equals(item.getSub_type())) {
            //补给站购买
            //金币是field1   数量是filed2  名称field3  类别field4   编号field5
            content = "花费" + item.getField1() + "金币购买了" + item.getField2() + "个" + item.getField3()+"。";
            //计算需要变色的文字位置
            int oneStart = 3;
            int oneEnd = oneStart + item.getField1().length();
            int twoStart = oneEnd + 5;
            int twoEnd = twoStart + item.getField2().length();
            setPkTextState(tvContent, content, oneStart, oneEnd, twoStart, twoEnd);
            //改变图标
            lv.setImageResource(R.drawable.st_notification_buy);
            //改变整体背景
            llOut.setBackgroundResource(R.drawable.st_notification_purchase);
        } else if ("4".equals(item.getSub_type())) {
            //宠物喂食
            content = "你的好友" + item.getName() + "给你的宠物喂了" + item.getField1() + "个" + item.getField2()+"。";
            //计算需要变色的文字
            int oneStart = 4;
            int oneEnd = oneStart + item.getName().length();
            int twoStart = oneEnd + 7;
            int twoEnd = twoStart + item.getField1().length();
            int threeStart = twoEnd + 1;
            int threeEnd = threeStart + item.getField2().length();
            setPkTextState(tvContent, content, oneStart, oneEnd, twoStart, twoEnd, threeStart, threeEnd);
            //改变图标
            lv.setImageResource(R.drawable.st_noti_feed);
            //改变整体背景
            llOut.setBackgroundResource(R.drawable.st_notification_feed);
        }
        //处理时间
        tvTime.setText(DateUtils.fromToday(item.getCreate_time()));
    }


    /**
     * 拜访好友设置文字为红色的方法 ,PK和购买
     *
     * @param tvContent
     * @param start
     * @param end
     */
    private void setRedColorForTv(TextView tvContent, int start, int end) {
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(ss);
    }


    /**
     * 对文字进行标红的方法,拜访
     *
     * @param tvContent
     * @param content
     */
    private void setPkTextState(TextView tvContent, String content, int otherStart, int otherEnd, int ownStart, int ownEnd) {
        SpannableString ss = new SpannableString(content);
        //计算对方的名称需要标红的起始坐标
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), otherStart, otherEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //计算对方的宠物名称标红的起始坐标
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), ownStart, ownEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(ss);
    }


    /**
     * 对文字进行标红的方法,需要标红三处文字的方法
     *
     * @param tvContent
     * @param content
     */
    private void setPkTextState(TextView tvContent, String content, int oneStart, int oneEnd, int twoStart, int twoEnd, int threeStart, int threeEnd) {
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), oneStart, oneEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), twoStart, twoEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), threeStart, threeEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvContent.setText(ss);
    }

}
